//
//  MovieListViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "AppTools.h"
#import "Movie.h"
#import "MovieDetailViewController.h"
#import "MBProgressHUD.h"

@interface MovieListViewController ()<UITableViewDataSource,UITableViewDelegate, UISearchResultsUpdating>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) MBProgressHUD *hub;
@property (nonatomic, retain) NSMutableArray *movieArray;
@property(nonatomic,retain)UISearchController *searchController;
@property (nonatomic, retain) NSMutableArray *searchArray;

@end

@implementation MovieListViewController

- (void)dealloc
{
    [_movieArray release];
    [_myTableView release];
    [_hub release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 55, 375, 667)];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    self.searchArray = [NSMutableArray array];
    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.searchResultsUpdater = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0);
    [self.searchController.searchBar sizeToFit];
    
    self.navigationItem.title = @"电影";
    self.myTableView.tableHeaderView = self.searchController.searchBar;
    
    self.movieArray = [NSMutableArray array];
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    [self.myTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"movie"];
    
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"正在加载中";
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
       
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        
        if ([reason isEqualToString:@"success"]) {
            
            for (NSDictionary *dic in array) {
                
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.movieArray addObject:movie];
                [movie release];
                
            }
            
            [self.myTableView reloadData];
            [self.hub hide:YES];
            
        }else{
            
            NSLog(@"请求失败");
            
        }

    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.searchController.active) {
        
        return self.searchArray.count;
    }
    else {
        return self.movieArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (self.searchController.active) {
        Movie *mov = [self.searchArray objectAtIndex:indexPath.row];
        cell.myMovie = mov;
    }else {
        Movie *mov = [self.movieArray objectAtIndex:indexPath.row];
        cell.myMovie = mov;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MovieDetailViewController *movieDVC = [[MovieDetailViewController alloc] init];
    Movie *movie = nil;
    
    if (self.searchController.active) {
        
        NSLog(@"选中搜索列表");
        movie = [self.searchArray objectAtIndex:indexPath.row];
    }
    else {
        
        NSLog(@"选中所有列表");
        movie = [self.movieArray objectAtIndex:indexPath.row];
    }
    movieDVC.movieID = movie.movieId;
    movieDVC.movieName = movie.movieName;
    
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    [movieDVC release];
}

-(void)updateSearchResultsForSearchController:(UISearchController *)searchController {
    NSString *searchString = [self.searchController.searchBar text];
    
    NSLog(@"===%@",searchString);
    
    [self.searchArray removeAllObjects];
    for (Movie *movie in self.movieArray) {
        
        //rangeOfString:方法是在movieName中搜索是否包含searchText
        //rangeOfString 前面的参数是要被搜索的字符串，后面的是要搜索的字符
        //NSNotFound 表示请求操作的某个内容没有发现
        if ([movie.movieName rangeOfString:searchString].location != NSNotFound) {
            
            [self.searchArray addObject:movie];
        }
    }
    NSLog(@"searchArray===%@",self.searchArray);
    
    [self.myTableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (self.searchController.active) {
        self.searchController.active = NO;
        [self.searchController.searchBar removeFromSuperview];
    }
}

- (void)buttonAction:(UIBarButtonItem *)btn
{
    
}
    
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

